﻿using System.Windows.Controls;
using System.Windows;
using System.Windows.Media.Imaging;
using System.Linq.Expressions;
using System;
using System.Windows.Input;
using DeepEarth.Core.Utilities;
namespace DeepEarth.Map.Core.Utilities
{
    public static class ContextMenuHelper
    {
        public static void ClearContextMenu(UIElement element)
        {
            var cm = element.GetValue(ContextMenuService.ContextMenuProperty) as ContextMenu;

            if (cm != null)
            {
                cm.Items.Clear();
            }
        }

        public static void RemoveContextMenu(UIElement element)
        {
            element.SetValue(ContextMenuService.ContextMenuProperty, null);
        }

        public static void AddDeleteMenuItem(UIElement element, ICommand removeElementCommand, object parameter)
        {
            Image image = new Image()
            {
                Width = 22,
                Height = 22
            };
            image.Source = new BitmapImage(new Uri("/DeepEarth.Map.Core;component/Images/delete.png", UriKind.Relative));

            AddMenuItem(element, "Delete", image, removeElementCommand, parameter);
        }

        public static void AddClipboardCommand(UIElement element, string header, Func<string> toStringFunction)
        {
            Image image = new Image()
            {
                Width = 22,
                Height = 22
            };
            image.Source = new BitmapImage(new Uri("/DeepEarth.Map.Core;component/Images/clipboard_download.png", UriKind.Relative));

            LambdaCommand command = new LambdaCommand(() =>
                {
                    string text = toStringFunction();
                    
                    // User will get a dialog asking to enable clipboard access.
                    // An exception will be thrown if access is denied
                    try
                    {
                        Clipboard.SetText(text);
                    }
                    catch { }

                }
            );

            AddMenuItem(element, header, image, command, null); 
        }

        public static void AddEditMenuItem(UIElement element, ICommand editCommand)
        {
            Image image = new Image()
            {
                Width = 22,
                Height = 22
            };
            image.Source = new BitmapImage(new Uri("/DeepEarth.Map.Core;component/Images/UEB32.png", UriKind.Relative));

            AddMenuItem(element, "Edit", image, editCommand, null); 
        }

        private static ContextMenu GetContextMenu(UIElement element)
        {
            var cm = element.GetValue(ContextMenuService.ContextMenuProperty) as ContextMenu;

            if (cm == null)
            {
                cm = new ContextMenu();
                element.SetValue(ContextMenuService.ContextMenuProperty, cm);
            }

            return cm;
        }

        private static void AddMenuItem(UIElement element, string headerText, object Icon, ICommand command, object parameter)
        {
            ContextMenu cm = GetContextMenu(element);

            MenuItem mi = new MenuItem
            {
                Header = headerText,
                Icon = Icon
            };

            cm.Items.Add(mi);
            mi.Command = command;
            mi.CommandParameter = parameter;           
        }
    }
}
